// pages/company-detail/company-detail.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyInfo:{},
    positionInfo:{},
    cityName:"",
    artbanners:{},
    companyvideos:{},
    currentTab: 0, // tab切换 
    flag1:0,
    flag2:0,
    flag3:0,
    flag4:0,
    flag5:0,
    flag6:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var companyId = options.id;
    if(options.currentTab){
      this.setData({
        currentTab:1
      })
    }
    var that=this;
    util.post(app.api.getCompanyByVideo, { companyId: companyId, media_type: 2},function(res){
      that.setData({
        companyvideos:res.data
      })
    })
    //获取文章
    util.post(app.api.getCompanyByVideo, { companyId: companyId, media_type:1},function(res){
      that.setData({
        artbanners:res.data
      })
    });
    util.post(app.api.companyDetail, { id: companyId }, function (res) {
      //获取公司信息
      var f1=0,f2=0,f3=0,f4=0,f5=0,f6=0;
      console.log("companyDetail:"+res.data);
      if (res.data!=null) {
        var trends = res.data.companyTrends;
        console.log("Trends:"+trends);
        if(trends!=null) {
          for(var val of trends){
            console.log(val+"\n");
            if (val.typeNo == 1) {
              f1=1;
            } else if (val.typeNo == 2) {
              f2=1;
            } else if (val.typeNo == 3) {
              f3=1;
            } else if (val.typeNo == 4) {
              f4=1;
            } else if (val.typeNo == 5) {
              f5=1;
            } else if (val.typeNo == 6) {
              f6=1;
            }
          }
        }
      }
      that.setData({
        companyInfo: res.data,
        flag1:f1,
        flag2:f2,
        flag3:f3,
        flag4:f4,
        flag5:f5,
        flag6:f6
      });
   
    });
    //获取公司职位
    util.post(app.api.getJobdescByCompany, { companyId: companyId},function(res){
      if (res.data != null) {
        res.data.forEach(function(item, index){
          item.createDate = util.date_format_time(item.createDate);
        })
        that.setData({
          positionInfo: res.data,
        });
      }
       
    })
    util.post(app.api.getCompanyByVideo, { companyId: companyId, media_type:2 }, function (res) {
      that.setData({
        companyvideos: res.data
      })
    });
  },
  
  //职位详情
  positionDetailTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    // 切换页面
    wx.navigateTo({
      url: '../position-detail/position-detail?pid=' + id
    });
  },

  //公司精选文章
  companyDetailTap: function (event) {
    var id = event.currentTarget.dataset.url; // 当前id
    wx.navigateTo({
      url: '../leader/stories/stories?url=' + id
    });
  },
  //跳转微信文章
  artTap: function (event) {
    
    var url = encodeURIComponent(event.currentTarget.dataset.url); // 当前id
    // 切换页面
    wx.navigateTo({
      url: '../leader/stories/stories?url=' + url
    });
  },
  //分享
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
     
    }
    var title = that.data.companyInfo.name;
    return {
      title: title,
      path: 'pages/company-detail/company-detail?id=' + that.data.companyInfo.id,
      data: that.data.companyInfo,
      success: function (res) {
        // 转发成功
        // console.log(res);
      },
      fail: function (res) {
        // console.log(res)
        // 转发失败
      }
    }
  },

  //点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }

  },

})